package com.ylyq.lingoserver.util;


import com.ylyq.lingoserver.constants.AccountConstant;

import java.io.*;
import java.util.Map;

/**
 * @ClassName BufferedWriterUtil
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/7/15 17:51
 * @Version 1.0
 */
public class BufferedWriterUtil {
  /**
   * @Author xiaweichao
   * @Description //TODO 把val字符串写入file文件中 * @Date 17:52 2020/7/15
   * @Param [lingoFile, val]
   * @return java.lang.String
   **/
    public static int writerIntoFile(File lingoFile, String val) {

        try {
            if (!lingoFile.exists()) {
                lingoFile.createNewFile();
            }

            BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(lingoFile, true)));
//            BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(lingoFile, true), "UTF-8"));
            //FileWriter fw = new FileWriter(lingoFile);

            fw.write(val);

            fw.flush();
            fw.close();

            return AccountConstant.SUCCESS;

        } catch (Exception ex) {
            ex.printStackTrace();
            return AccountConstant.FAIL;
        }
    }


    /**
     * @Author xiaweichao
     * @Description //TODO 从file文件中读取里面的内容并返回 * @Date 18:09 2020/7/15
     * @Param [file]
     * @return java.lang.String
     **/
    public static String readFromFile(File file) {
        StringBuffer sb=new StringBuffer();
        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream("lingo/lingosh.lng"));
//            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("pkg/b.txt"));

            byte[] b = new byte[1024];
            int len = -1;
            while ((len = in.read(b, 0, b.length)) != -1) {
                String str = new String(b, 0, len, "UTF-8");

                sb.append(str);

//                System.out.println(str);
//                out.write(b, 0, len);
            }
            //刷新缓冲
//            out.flush();
            //关闭流对象
            in.close();
//            out.close();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return sb.toString();
    }

    /**
     * @Author xiaweichao
     * @Description //TODO 从oldFilePath文件中读取里面的内容并输出到newFilePath中，输出时也可替换指定的字符 * @Date 18:09 2020/7/15
     * @Param [file]    replaceChar中 key是old要被替换的，value是new目标替换的
     * @return java.lang.String
     **/
    public static Integer replaceStringFromFileToFile(String oldFilePath, String newFilePath, Map<String,String> replaceChar) {
        StringBuffer sb=new StringBuffer();
        try {
            File file=new File(newFilePath);
            if (!file.exists()) {
                file.createNewFile();
            }

            BufferedInputStream in = new BufferedInputStream(new FileInputStream(oldFilePath));
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFilePath,true));

            byte[] b = new byte[1024];
            int len = -1;
            while ((len = in.read(b, 0, b.length)) != -1) {
//                String str = new String(b, 0, len, "UTF-8");
                String str = new String(b, 0, len);
                sb.append(str);
            }
            String result = sb.toString();
            for (Map.Entry<String,String> entry:replaceChar.entrySet()) {
                result=result.replace(entry.getKey(),entry.getValue());
            }
            out.write(result.getBytes());
            //刷新缓冲
            out.flush();
            //关闭流对象
            in.close();
            out.close();
            return AccountConstant.SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return AccountConstant.FAIL;
        }

    }

}
